package com.example.block_chain.service.impl.award;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.block_chain.domain.dto.AwardDto;
import com.example.block_chain.domain.pojo.Award;
import com.example.block_chain.domain.vo.AwardVo;
import com.example.block_chain.mapper.AwardMapper;
import com.example.block_chain.service.admin.AwardAuditingService;
import com.example.block_chain.service.award.AwardService;
import com.example.block_chain.service.blockChain.IpfsService;
import com.example.block_chain.service.OSSUploadService;
import com.example.block_chain.utils.PathUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 奖项表(Award)表服务实现类
 *
 * @author makejava
 * @since 2023-11-12 16:43:15
 */
@Service("awardService")
public class AwardServiceImpl extends ServiceImpl<AwardMapper, Award> implements AwardService {
    @Resource
    OSSUploadService ossUploadService;

    @Resource
    IpfsService ipfsService;

    @Resource
    AwardAuditingService awardAuditingService;

    @Override
    public void saveAward(AwardDto awardDto) throws IOException {
        Award award = new Award();
        BeanUtils.copyProperties(awardDto, award);
        MultipartFile file = awardDto.getFile();
        String fileDir = PathUtil.generateFileDir(awardDto.getSchoolCode());
        String url = ossUploadService.upload(file.getInputStream(), fileDir);
        String filePath = PathUtil.generateFilePath(awardDto.getSchoolCode(), Objects.requireNonNull(file.getOriginalFilename()));
        String fileCid = ipfsService.uploadFileFromStream(filePath, file.getInputStream());
        award.setAwardUrl(url);
        award.setDigest(fileCid);
        award.setCreateTime(new Date());
        save(award);
    }

    @Override
    public Award getAward(Long id) throws UnsupportedEncodingException {
        Award award = getById(id);
        List<String> list = new ArrayList<>();
        list.add(award.getAwardUrl());
        List<String> imagesUrl = ossUploadService.getImagesUrl(list);
        award.setAwardUrl(imagesUrl.get(0));
        List<Award> awards = new ArrayList<>();
        awards.add(award);
        awardAuditingService.toAwardVo(awards);
        return award;
    }

    @Override
    public AwardVo getAwardVoById(Long id) throws UnsupportedEncodingException {
        Award award = getById(id);
        if (award == null) {
            return null;
        }
        List<String> list = new ArrayList<>();
        list.add(award.getAwardUrl());
        List<String> imagesUrl = ossUploadService.getImagesUrl(list);
        award.setAwardUrl(imagesUrl.get(0));
        List<Award> awards = new ArrayList<>();
        awards.add(award);
        return awardAuditingService.toAwardVo(awards).get(0);
    }

    @Override
    public Page<AwardVo> getAwards(String sNo, String schoolCode, Integer page, Integer pageSize) throws UnsupportedEncodingException {
        Page<Award> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Award> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Award::getSNo, sNo).eq(Award::getSchoolCode, schoolCode);
        queryWrapper.orderByDesc(Award::getCreateTime);
        Page<Award> p = page(pageInfo, queryWrapper);

        List<String> list = new ArrayList<>();
        List<Award> awards = p.getRecords();
        getAwardPictureUrl(list, awards, ossUploadService);
        List<AwardVo> awardVos = awardAuditingService.toAwardVo(awards);
        Page<AwardVo> awardVoPage = new Page<>();
        p.setRecords(awards);
        BeanUtils.copyProperties(p, awardVoPage);
        awardVoPage.setRecords(awardVos);
        return awardVoPage;
    }

    public static void getAwardPictureUrl(List<String> list, List<Award> awards, OSSUploadService ossUploadService) throws UnsupportedEncodingException {
        awards.forEach(award -> list.add(award.getAwardUrl()));
        List<String> imagesUrl = ossUploadService.getImagesUrl(list);
        for (int i = 0; i < awards.size(); i++) {
            awards.get(i).setAwardUrl(imagesUrl.get(i));
        }
    }
}
